Security News
Python Overtakes JavaScript as Top Programming Language on GitHub
Python becomes GitHub's top language in 2024, driven by AI and data science projects, while AI-powered security tools are gaining adoption.
import-from-esm
Advanced tools
Import a module like with require() but from a given path (for ESM)
Import a module like with
require()
but from a given path (for ESM)
This library intends to be an almost drop-in replacement of import-from
(from which it is forked), exposing the same API and behavior but also supporting ES modules (ESM). Just add await
before importFrom
/importFrom.silent
The main benefit of using import-from
is that it abstracts the need to resolve the path and create a require
statement. Its code is really straightforward:
(fromDirectory, moduleId) => createRequire(path.resolve(fromDirectory, "noop.js"))(moduleId);
In the case of import-from-esm
, there are a few additional benefits because of the way ESM works:
import.meta.resolve
, which is behind an experimental flag (although there's a ponyfill available at wooorm/import-meta-resolve, which import-from-esm
uses under-the-hood).require
(while the former is still in experimental).import-from-esm
re-introduces require
's file extension discovery.As you can see, there is quite a bit of complexity that is abstracted behind import-from-esm
. The first bullet point issue affected both @semantic-release/commit-analyzer
and @semantic-release/release-notes-generator
. After spending hours on research to solve the issue, I realized that the work I was doing would benefit others as well, so I decided to create a package out of it.
As a proponent of ESM, I have put a lot of thought into poly-filling require
features for import
, but finally came to the conclusion that developing a package to facilitate the ecosystem transition to ESM by reducing friction was a good thing.
$ npm install import-from-esm
import importFrom from "import-from-esm";
// there is a file at `./foo/bar.{js,mjs,cjs,json}`
await importFrom("foo", "./bar");
Like require()
, throws when the module can't be found.
Returns undefined
instead of throwing when the module can't be found.
Type: string
Directory to import from.
Type: string
What you would use in require()
.
Create a partial using a bound function if you want to import from the same fromDir
multiple times:
const importFromFoo = importFrom.bind(null, "foo");
importFromFoo("./bar");
importFromFoo("./baz");
1.3.4 (2024-04-15)
FAQs
Import a module like with require() but from a given path (for ESM)
The npm package import-from-esm receives a total of 726,210 weekly downloads. As such, import-from-esm popularity was classified as popular.
We found that import-from-esm demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Python becomes GitHub's top language in 2024, driven by AI and data science projects, while AI-powered security tools are gaining adoption.
Security News
Dutch National Police and FBI dismantle Redline and Meta infostealer malware-as-a-service operations in Operation Magnus, seizing servers and source code.
Research
Security News
Socket is tracking a new trend where malicious actors are now exploiting the popularity of LLM research to spread malware through seemingly useful open source packages.